package study.week05.hikari;

import study.week05.entity.TestStudent;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class HikariTest {

    public static void main(String[] args) throws Exception {
        String driver = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&transformedBitIsBoolean=true&autoReconnect=true&serverTimezone=Asia/Shanghai&useCursorFetch=true";
        String user = "root";
        String pwd = "123456";
        StudentHikariUtil util = new StudentHikariUtil(driver, url, user, pwd);

        //新增的对象
        TestStudent stu1 = new TestStudent();
        stu1.setStudentName("stu1");
        stu1.setStudentDetail("{\"address\":\"zz路\",\"age\":10}");
        TestStudent stu2 = new TestStudent();
        stu2.setStudentName("stu2");
        stu2.setStudentDetail("{\"address\":\"xx路\",\"age\":20}");
        TestStudent stu3 = new TestStudent();
        stu3.setStudentName("stu3");
        stu3.setStudentDetail("{\"address\":\"yy路\",\"age\":30}");
        //清空
        util.truncate();
        //新增
        util.addUser(stu1);
        util.addUser(stu2);
        util.addUser(stu3);
        //列表
        displayList("新增后",util);
        //更新
        TestStudent update = new TestStudent();
        update.setId(2L);
        update.setStudentName("stu22");
        update.setStudentDetail("{\"address\":\"xxxxx路\",\"age\":22}");
        util.updateUser(update);
        //根据ID删除
        util.deleteById(3L);
        //列表
        displayList("更新和删除后",util);

        //测试事务成功
        ArrayList<TestStudent> transactionSuccess = new ArrayList<>();
        TestStudent updateTran2 = new TestStudent();
        updateTran2.setId(1L);
        updateTran2.setStudentName("stu11");
        updateTran2.setStudentDetail("{\"address\":\"11路\",\"age\":26}");
        TestStudent updateTran1 = new TestStudent();
        updateTran1.setId(2L);
        updateTran1.setStudentName("stu222");
        updateTran1.setStudentDetail("{\"address\":\"22路\",\"age\":25}");
        transactionSuccess.add(updateTran1);
        transactionSuccess.add(updateTran2);
        boolean res = util.updateUserTransaction(transactionSuccess);
        System.out.println("测试事务成功 结果:"+res);
        displayList("事务成功后",util);


        //测试事务成功
        ArrayList<TestStudent> transactionfail = new ArrayList<>();
        TestStudent updateTran22 = new TestStudent();
        updateTran22.setId(1L);
        updateTran22.setStudentName("stu11");
        updateTran22.setStudentDetail("{\"address\":\"1111111路\",\"age\":2666}");
        TestStudent updateTran11 = new TestStudent();
        updateTran11.setId(2L);
        //生成300长度的字符串,超出表结构的限制, 会修改失败
        updateTran11.setStudentName(genLongStr(300));
        updateTran11.setStudentDetail("{\"address\":\"2222222路\",\"age\":2555}");
        transactionfail.add(updateTran11);
        transactionfail.add(updateTran22);
        res = util.updateUserTransaction(transactionfail);
        System.out.println("测试事务失败 结果:"+res);
        //测试事务失败
        displayList("事务失败后",util);
        //测试批量新增
        List<TestStudent> batchList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            TestStudent batch = new TestStudent();
            batch.setStudentName("stu-batch-"+i);
            batch.setStudentDetail("{\"address\":\""+i+"路\",\"age\":"+i+"}");
            batchList.add(batch);
        }
        util.addUserBatch(batchList);
        displayList("批量新增后",util);

    }
    private static String genLongStr(int len ){
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < len; i++) {
            res.append("a");
        }
        return res.toString();
    }
    private static void displayList(String str,StudentHikariUtil util) throws SQLException {
        List<TestStudent> testStudents = util.queryList();
        System.out.println("=========="+str);
        System.out.println(testStudents);
    }
}
